<template>
        <div class>
            <el-card class="ep-card ep-search">
                <el-row :gutter="0">
                  <el-col :span="2" style="float:right">
                    <el-button type="success"  @click="save">保存</el-button>
                  </el-col>
                </el-row>
              </el-card>
             <el-tabs type="border-card">
              <el-tab-pane label="企业贷款情况">
                <div class>
                    <el-table :data="tableData" border style="width: 100%">
                        <el-table-column  align="center" width="80">
                            <template slot="header">
                            <el-button class="el-icon-circle-plus" @click.prevent="addRow()"></el-button>
                            </template>
                            <template slot-scope="scope">
                            <el-button size="mini" type="danger" class="el-icon-remove"
                            @click="handleDelete(scope.$index, scope.row)"></el-button>
                        </template>
                        </el-table-column>
                        <el-table-column
                            type="index"
                            width="50" label="NO.">
                        </el-table-column>
                        <el-table-column label="年度" align="center">
                            <template slot-scope="scope">
                            <el-date-picker type="year"  format="yyyy" value-format="yyyy" placeholder="选择日期" v-model="scope.row.year" style="width: 100%;" :editable="false"></el-date-picker>
                            </template>
                        </el-table-column>
                        <el-table-column label="销售收入(万元)" align="center">
                        <template slot-scope="scope">
                        <el-input v-model="scope.row.saleReven"></el-input>
                        </template>
                        </el-table-column>
                        <el-table-column label="净利润(万元)" align="center">
                            <template slot-scope="scope">
                            <el-input v-model="scope.row.netProfit"></el-input>
                            </template>
                        </el-table-column>
                    </el-table>
               </div>
              </el-tab-pane>
            </el-tabs>
            </div>
        </template>
        
        <script>
        export default {
          components: {},
          data() {
            return {
             tableData: [],
             projId:'',
             isSaveOrUpdate:false,
            };
          },
          computed: {},
          mounted() {
         
          },
          methods: {
            init(projId){
                  this.projId=projId;
                  this.$api.formPost( 'biz/projEnterLoanSitu/selectEnterLoanSituByProjId',{
                    projId:projId
                  }).then(res => {
                    if(res.retHead.code=="0000000"){
                        let isSaveOrUpdate=false;
                        if(res.retBody.enterLoanSituList){
                          this.tableData=res.retBody.enterLoanSituList;
                           isSaveOrUpdate=true;
                        }
                        this.isSaveOrUpdate=isSaveOrUpdate;
                    }else{
                        this.$message({
                            type: 'error',
                            message: res.retHead.message
                        });
                    }
                });
            },
            save(){
              let user=JSON.parse(sessionStorage.getItem("user"));
              let params={
                  createUserId:user.id,
                  createUserName:user.realName,
                  projId:this.projId,
                  enterLoanSituList:this.tableData,
              }
              if(!this.isSaveOrUpdate){
                    this.$api.post( 'biz/projEnterLoanSitu/doSaveEnterLoanSitu',params).then(res => {
                        if(res.retHead.code=="0000000"){
                              this.$message({
                                  type: 'success',
                                  message: "保存成功！"
                              });
                              this.tableData=[];
                              this.$emit("hiddenComponent");
                          }else{
                          
                              this.$message({
                                  type: 'error',
                                  message: res.retHead.message
                              });
                          }
                  });
              }else{
                //更新
                this.$api.post( 'biz/projEnterLoanSitu/doUpdateEnterLoanSitu',params).then(res => {
                        if(res.retHead.code=="0000000"){
                              this.$message({
                                  type: 'success',
                                  message: "更新成功！"
                              });
                              this.tableData=[];
                              this.$emit("hiddenComponent");
                          }else{
                          
                              this.$message({
                                  type: 'error',
                                  message: res.retHead.message
                              });
                          }
                  });
              }
              
            },
            getdata(){
              return this.tableData
            },
            // 增加行
            addRow() {
              var list = {
                  effectiveTerm:"0.00"
              }
              this.tableData.unshift(list)
            },
            handleDelete(index, row) {
                //删除数据库
                  if(row.id){
                    this.$confirm('此操作将永久删除该条记录, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                      }).then(() => {
                        //删除
                        this.$api.formPost( 'biz/projEnterLoanSitu/doRmById',{
                          id:row.id
                          }).then(res => {
                              if(res.retHead.code=="0000000"){
                                    this.$message({
                                      type: 'success',
                                      message: '删除成功!'
                                    });
                                    this.tableData.splice(index, 1);
                                }else{
                                    this.$message({
                                        type: 'error',
                                        message: res.retHead.message
                                    });
                                }
                          });
                      }).catch(() => {
                        this.$message({
                          type: 'info',
                          message: '已取消删除'
                        });          
                      });
                  }else{
                    this.tableData.splice(index, 1);
                  }
            }
        
          },
          created() {}
        };
        </script>
        <style lang='scss' scoped>
        
        </style>